package com.carbeauty.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.carbeauty.daoimpl.CarColorDaoImpl;
import com.carbeauty.image.Constants;
import com.carbeauty.model.CarColor;
import com.carbeauty.util.FileUtil;
import com.carbeauty.web.listener.ImageIndexCreateListener;

public class CarColorController extends MultiActionController{
	
	private CarColorDaoImpl carColorService;
	
    public CarColorDaoImpl getCarColorService() {
		return carColorService;
	}

	public void setCarColorService(CarColorDaoImpl carColorService) {
		this.carColorService = carColorService;
	}

	public ModelAndView carColorlist(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        String userId = ServletRequestUtils.getStringParameter(request,
                "userId", "xing");
        List<CarColor> carColorList=carColorService.getAllCarColor();

     	mav.addObject("alist", carColorList);

        mav.addObject("userId", userId);

        mav.setViewName("pages/carColorManager");
        return mav;
    }
    
	public ModelAndView modify(HttpServletRequest request,HttpServletResponse response)
			throws Exception{
		
		ModelAndView Mav = new ModelAndView();
		String colorId = request.getParameter("colorId");
		//String aa=ServletRequestUtils.getStringParameter(request, "colorId");
		List<CarColor> carColorObj=carColorService.getCarColorById(colorId);
		Mav.addObject("colorObj", carColorObj);
		Mav.setViewName("pages/carColorInfo");
		return Mav;
	}
	
	public ModelAndView add(HttpServletRequest request,HttpServletResponse response){
		ModelAndView Mav = new ModelAndView();
		request.setAttribute("pageFlag", "AddPage");
		Mav.setViewName("pages/carColorInfo");
		return Mav;
	}
	
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response)
			throws Exception{
		
		ModelAndView Mav = new ModelAndView();
		String colorIdstr = request.getParameter("colorId");
		Long colorId = null;
		if(colorIdstr!=null&&!"".equals(colorIdstr))
			colorId = Long.valueOf(colorIdstr);
		String colorName = request.getParameter("colorName");
		String userId = request.getParameter("userId");
		String rgbValue = request.getParameter("rgbValue");
		String state = request.getParameter("state");
		//System.out.println("save=="+colorId);
		String imageLocation = request.getParameter("imageLocation");//uploadFile(request, response);
		String fileName = imageLocation.startsWith("Temp_")?imageLocation.substring(5, imageLocation.length()):imageLocation;
		CarColor carColorObj = new CarColor();
		carColorObj.setId(colorId);
		carColorObj.setColorName(colorName);
		carColorObj.setUserId(userId);
		carColorObj.setRgbValue(rgbValue);
		carColorObj.setState(state);
		carColorObj.setImageLocation(fileName);
		carColorObj.setFileName(fileName);
		if(imageLocation!=null &&!"".equals(imageLocation.trim())&&!"null".equals(imageLocation.trim())){
			String fileName2 = Constants.FILE_UPLOAD_DIR+File.separator+fileName;
			if(imageLocation.startsWith("Temp_"))
			{
				String tempFile = Constants.FILE_UPLOAD_TEMP_DIR+File.separator+imageLocation;
				System.out.println(tempFile);
				System.out.println(fileName2);
				FileUtil.copyFileToNewPath(tempFile, fileName2);
				ImageIndexCreateListener.getImageIndexThread().createImageIndex(fileName2);
				FileUtil.deleteFile(tempFile);
			}else{
				ImageIndexCreateListener.getImageIndexThread().createImageIndex(fileName2);
			}
		}
		carColorService.saveCarColorObj(carColorObj);

		return carColorlist(request, response);
	}
	
	
    
}

